<?php
class MenuBuilder{
	var $ls = array();
	var $mainid = 'main';
	var $s = array();
	function __construct($settings){
		$this->MenuBuilder($settings);
	}
	function MenuBuilder($settings){
		$this->s=$settings;
		//Creating the defaults, should be overwritten by a stylefile
		$this->ls['menuOrientation']='horizontal';
		$this->ls['menuCurrentStyle']=false;
		$this->ls['submenuIndicatorType']='text'; // this is text or image
		$this->ls['submenuIndicatorVar']='>'; // can be something like > or the url to an image
		$this->ls[$this->mainid.'_width']='150px';
		$this->ls[$this->mainid.'_height']='20px';
		$this->ls[$this->mainid.'_padding']='3px';
		$this->ls[$this->mainid.'_distance']='0px';
		$this->ls[$this->mainid.'_border_size']='1px';
		$this->ls[$this->mainid.'_border_type']='solid';
		$this->ls[$this->mainid.'_border_color']='#000';
		$this->ls[$this->mainid.'_background_color']='';
		$this->ls[$this->mainid.'_background_image_url']='';
		$this->ls[$this->mainid.'_background_image_offset_x']='0';
		$this->ls[$this->mainid.'_background_image_offset_y']='0';
		$this->ls[$this->mainid.'_background_image_repeat']='no-repeat';
		$this->ls[$this->mainid.'_font_size']='';
		$this->ls[$this->mainid.'_font_color']='';
		$this->ls[$this->mainid.'_font_family']='';
		$this->ls[$this->mainid.'_font_weight']='normal';
		$this->ls[$this->mainid.'_font_decoration']='none';
		$this->ls[$this->mainid.'_text_alignment']='center';
		$this->ls[$this->mainid.'_opacity']='1';

		$this->setCssValues();
	}
	function setCssValues(){
		$cssString = file_get_contents($this->s['cssfile']);
		$cssLines = explode("\n",$cssString);
		array_shift($cssLines);
		foreach($cssLines as $l){
			if($l=="MenuBuilderVars*/") break;
			$this->ls[substr($l,0,strpos($l,':'))]=trim(substr($l,strpos($l,':')+1));
			//print $l;
		}

	}
	function getOption($id, $indication, $value){
		if(array_key_exists($id.$indication,$this->ls)&&$this->ls[$id.$indication]==$value){
			return 'selected';
		}elseif(array_key_exists($this->mainid.$indication,$this->ls) && $this->ls[$this->mainid.$indication]==$value){
			return 'selected';
		}else{
			return '';
		}
	}
	function getValue($id,$indication=''){
		if(array_key_exists($id.$indication,$this->ls)){
			return $this->ls[$id.$indication];
		}else{
			return $this->ls[$this->mainid.$indication];
		}
	}
	function getChecker($id, $indication, $main){
		return '<input type="checkbox" id="'.$id.$indication.'_checker" '.(array_key_exists($id.$indication,$this->ls)?'checked':'').' '.($main?'disabled':'').' />';
	}
	function getLayoutControls($id, $active=false, $main=false){
		$code='
		<table>';
		if(!$active){$code.='
			<tr>
				<td>
					'.$this->getChecker($id,'_width', $main).'Width:</td>
				<td>
					<input type="text" size="5" id="'.$id.'_width" value="'.$this->getValue($id, '_width').'" />
				</td>
			</tr>
			<tr>
				<td>'.$this->getChecker($id, '_height',$main).'Height:</td>
				<td>
					<input type="text" size="5" id="'.$id.'_height" value="'.$this->getValue($id, '_height').'" />
				</td>
			</tr>
			<tr>
				<td>
					'.$this->getChecker($id,'_padding', $main).'Padding:</td>
				<td>
					<input type="text" size="5" id="'.$id.'_padding" value="'.$this->getValue($id, '_padding').'" />
				</td>
			</tr>
			<tr>
				<td>
					'.$this->getChecker($id,'_distance', $main).'Distance:</td>
				<td>
					<input type="text" size="5" id="'.$id.'_distance" value="'.$this->getValue($id, '_distance').'" />
				</td>
			</tr>';
		}$code.='
			<tr>
				<td>
					'.$this->getChecker($id, '_border_size',$main).'Border:</td>
				<td>
					<input type="text" size="3" id="'.$id.'_border_size" value="'.$this->getValue($id, '_border_size').'" />px 
					<select id="'.$id.'_border_type">
							<option value="solid" '.$this->getOption($id,'_border_type','solid').'>solid</option>
							<option value="dotted" '.$this->getOption($id,'_border_type','dotted').'>dotted</option>
							<option value="dashed" '.$this->getOption($id,'_border_type','dashed').'>dashed</option>
					</select>
					color: <input type="text" size="7" id="'.$id.'_border_color" value="'.$this->getValue($id,'_border_color').'" style="background-color:'.$this->getValue($id,'_border_color').';"/>'.$this->colorPicker($id.'_border_color').'
				</td>
			</tr>
			<tr>
				<td>'.$this->getChecker($id,'_background_color',$main).'Background color: </td>
				<td>
					<input type="text" size="7" id="'.$id.'_background_color" value="'.$this->getValue($id,'_background_color').'" style="background-color:'.$this->getValue($id,'_background_color').';"/>
					'.$this->colorPicker($id.'_background_color').'
				</td>
			</tr>
			<tr>
				<td>'.$this->getChecker($id, '_background_image_url', $main).'Background image: </td>
				<td>
					<input type="text" size="7" id="'.$id.'_background_image_url" value="'.$this->getValue($id, '_background_image_url', $main).'" />
					'.$this->backgroundPicker($id.'_background_image_url').',
					y-offset:<input type="text" size="3" id="'.$id.'_background_image_offset_y" value="'.$this->getValue($id, '_background_image_offset_y',$main).'" />, 
					x-offset:<input type="text" size="3" id="'.$id.'_background_image_offset_x" value="'.$this->getValue($id, '_background_image_offset_x',$main).'" />, 
					repeat:
						<select id="'.$id.'_background_image_repeat">
							<option value="no-repeat" '.$this->getOption($id,'_background_image_repeat','no-repeat').'>no</option>
							<option value="repeat" '.$this->getOption($id,'_background_image_repeat','repeat').'>tile</option>	
							<option value="repeat-x" '.$this->getOption($id,'_background_image_repeat','repeat-x').'>x</option>
							<option value="repeat-y" '.$this->getOption($id,'_background_image_repeat','repeat-y').'>y</option>
						</select>
				</td>
			</tr>
			<tr>
				<td>'.$this->getChecker($id,'_font_size',$main).'Font: </td>
				<td>
					size:<input type="text" size="3" id="'.$id.'_font_size" value="'.$this->getValue($id, '_font_size').'" />, 
					<select id="'.$id.'_font_weight">
						<option value="normal" '.$this->getOption($id,'_font_weight','normal').'>normal</option>
						<option value="bold" '.$this->getOption($id,'_font_weight','bold').'>bold</option>
					</select>,
					<select id="'.$id.'_font_decoration">
						<option value="none" '.$this->getOption($id,'_font_decoration','none').'>Plain</option>
						<option value="underline" '.$this->getOption($id,'_font_decoration','underline').'>Line under</option>
						<option value="overline" '.$this->getOption($id,'_font_decoration','overline').'>Line over</option>
						<option value="line-through" '.$this->getOption($id,'_font_decoration','line-through').'>Line through</option>
						<option value="blink" '.$this->getOption($id,'_font_decoration','blink').'>blink</option>
					</select>,
					color:<input type="text" size="7"  id="'.$id.'_font_color" value="'.$this->getValue($id, '_font_color').'" style="background-color:'.$this->getValue($id,'_font_color').';"/>
					'.$this->colorPicker($id.'_font_color').', 
					family: <input type="text" size="10" id="'.$id.'_font_family" value="'.$this->getValue($id, '_font_family').'" />
				</td>
			</tr>
			<tr>
				<td>'.$this->getChecker($id,'_text_alignment',$main).'Position: </td>
				<td><select id="'.$id.'_text_alignment">
					<option value="left" '.$this->getOption($id,'_text_alignment','left').'>left</option>
					<option value="center" '.$this->getOption($id,'_text_alignment','center').'>center</option>
					<option value="right" '.$this->getOption($id,'_text_alignment','right').'>right</option>
					</select>
				</td>
			<tr>
				<td>'.$this->getChecker($id,'_opacity',$main).'opacity: </td>
				<td><input type="text" size="3"  id="'.$id.'_opacity" value="'.$this->getValue($id, '_opacity').'" /> (value between 0 and 1)</td>
			</tr>
		</table>';
		return $code;
	}
	function colorPicker($id){
		return '<button onclick="colorpick(\''.$id.'\')">Pick</button>';
	}
	function backgroundPicker($id){
		if(!strlen($this->s['backgroundPicker_url'])) return '';
		$js='window.SetUrl=function(value){document.getElementById(\''.$id.'\').value=value;};';
		$js.='window.open(\''.$this->s['backgroundPicker_url'].'\',\'kfm_window\',\'modal,width=700,height=500\');';
		return '<button onclick="'.$js.'">Browse</button>';
	}
}
?>
